package com.neo.imagepickerdemo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    ImageView ivImage;
    TextView tvPath;
    Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ivImage = findViewById(R.id.iv_image);
        tvPath = findViewById(R.id.tv_path);

        findViewById(R.id.btn_album).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ViewUtils.openGallery(MainActivity.this, 1);
            }
        });

        findViewById(R.id.btn_camera).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageUri =  ViewUtils.openCamera(MainActivity.this, 2);
            }
        });

        findViewById(R.id.btn_file).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ViewUtils.chooseFile(MainActivity.this, 3);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

            switch (requestCode) {
                case 1:
                    String path1 = ViewUtils.getAbsolutePath(this, data.getData());
                    tvPath.setText(path1);
                    ivImage.setImageURI(data.getData());
                    break;
                case 2:
                    tvPath.setText(imageUri.getPath());
                    ivImage.setImageURI(imageUri);
                    break;
                case 3:
                    String path3 = ViewUtils.getAbsolutePath(this, data.getData());
                    tvPath.setText(path3);
                    break;
                default:
                    break;
            }

        }
    }
}
